#import "AsyncImagesAppDelegate.h"

#import "BottomToolBar.h"
#import "ImageUIButton.h"
#import "TopBar.h"

#import "BaseInfoPanel.h"
#import "BaseDetailPanel.h"

#import "LetterButton.h"
#import "SHKItem.h"
#import "SHKActionSheet.h"

#import "LettersPanelComponent.h"
#import "LoginPanel.h"

#import "UtilClass.h"
#import "SearchPanel.h"

#define TABLE_ROW_HEIGHT 66

// TheClass.m
static bool need_draw_logo = FALSE;

@implementation AsyncImagesAppDelegate

@synthesize window;

#pragma mark -
#pragma mark Application lifecycle

#define SCREEN_WIDTH 320
#define SCREEN_HEIGHT 480

#define INFO_TABLE_HEIGHT 372

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{    
    // Override point for customization after app launch  
    [[UIApplication sharedApplication] setStatusBarHidden:YES];
    
    int pos_y = -20;
    
    navigationController = [[MyNavigationController alloc] initWithNibName:@"MyNavigationController" bundle:nil];
    window.rootViewController = navigationController;
    
    newView = [[BaseNaviPanel alloc] initWithFrame:CGRectMake(0, pos_y, SCREEN_WIDTH, SCREEN_HEIGHT)];
    [newView trigger:@"http://photo.zhulong.com/mobile/newest.asp"
      infoPanelTitle:@"筑龙建筑画报"
     detailPanelLink:@"http://photo.zhulong.com/mobile/detail.asp?id=" 
    detailPanelTitle:@"最新"
     suffix:@"new_panel"
     type:0];
     [navigationController.view addSubview:newView];

    worksView = [[BaseTabPanel alloc] initWithFrame:CGRectMake(0,pos_y, SCREEN_WIDTH, SCREEN_HEIGHT-50)];
    [worksView initWithTags:[[NSArray alloc] initWithObjects:@"作品分类",@"城市检索",@"热点", nil]
                         topBarTitle: @"建筑作品"
                            suffix:@"worksview"
                            table1url:@"http://photo.zhulong.com/mobile/pc.asp"
                            table2url:@"http://photo.zhulong.com/mobile/city.asp"
                            table3url:@"http://photo.zhulong.com/mobile/hot.asp"
                            table1_2_link:@"http://photo.zhulong.com/mobile/list.asp?t="
                           table2_2_link: @"http://photo.zhulong.com/mobile/clist.asp?c="
                        detal_table_link:@"http://photo.zhulong.com/mobile/detail.asp?id="];
    [window addSubview:worksView];

    peopleView = [[BaseNaviPanel alloc] initWithFrame:CGRectMake(0, pos_y, SCREEN_WIDTH, SCREEN_HEIGHT)];    
    [peopleView setAtomData:@"/mans/man"
                                     atom:@"man"
                                      feed:@"mans"];
    [peopleView configTable:SCREEN_WIDTH
                tableHeight:INFO_TABLE_HEIGHT+10
                 tableWidth:TABLE_ROW_HEIGHT
             tableUnitStyle:1];
    [peopleView setUpUseCharacterPanel];
    [peopleView configTweenTable:SCREEN_WIDTH
                     tableHeight:INFO_TABLE_HEIGHT+10
                      tableWidth:TABLE_ROW_HEIGHT
                  tableUnitStyle:1];
    [peopleView trigger:@"http://photo.zhulong.com/mobile/man_country.asp"
      infoPanelTitle:@"建筑人物"
     detailPanelLink:@"http://photo.zhulong.com/mobile/man_detail.asp?id=" 
    detailPanelTitle:@"建筑人物"
              suffix:@"people_panel"
               type:2];
    [peopleView setUpTweenTable:@"http://photo.zhulong.com/mobile/man.asp?c="];
    peopleView.tweenTableLink_Character = @"http://photo.zhulong.com/mobile/man.asp?Ch=";
    [peopleView setUpTweenAtomData:@"/mans/man" atom:@"man" feed:@"mans"];//@"/feed/man/projects/proj" atom:@"proj" feed:@"feed"
    [navigationController.view addSubview:peopleView];

    NSLog(@"GOGO");
    
    jigouView = [[BaseNaviPanel alloc] initWithFrame:CGRectMake(0, pos_y, SCREEN_WIDTH, SCREEN_HEIGHT)];    
    [jigouView setAtomData:@"/units/unit"
                       atom:@"unit"
                       feed:@"units"];
    [jigouView configTable:SCREEN_WIDTH
                tableHeight:INFO_TABLE_HEIGHT+10
                 tableWidth:TABLE_ROW_HEIGHT
             tableUnitStyle:1];
    [jigouView setUpUseCharacterPanel];
    [jigouView configTweenTable:SCREEN_WIDTH
                     tableHeight:INFO_TABLE_HEIGHT+10
                      tableWidth:TABLE_ROW_HEIGHT
                  tableUnitStyle:1];
    [jigouView trigger:@"http://photo.zhulong.com/mobile/unit_country.asp"//@"http://photo.zhulong.com/mobile/unit.asp?Ch=A"
         infoPanelTitle:@"建筑机构"
        detailPanelLink:@"http://photo.zhulong.com/mobile/unit_detail.asp?id=" 
       detailPanelTitle:@"建筑机构"
                 suffix:@"works_panel"
                  type:2];
    jigouView.tweenTableLink_Character = @"http://photo.zhulong.com/mobile/unit.asp?Ch=";
    [jigouView setUpTweenTable:@"http://photo.zhulong.com/mobile/unit.asp?c="];
    [jigouView setUpTweenAtomData:@"/feed/unit" atom:@"unit" feed:@"feed"];//@"/feed/unit/projects/proj" atom:@"proj" feed:@"feed"
    [navigationController.view addSubview:jigouView];
    
    moreView = [[MoreView alloc] initWithFrame:CGRectMake(0,pos_y, SCREEN_WIDTH, SCREEN_HEIGHT)];
    [navigationController.view addSubview:moreView];
    
    [self hideAllUIs];
    newView.hidden = FALSE;
    
    BottomToolBar *bottomToolBar = [[BottomToolBar alloc] initWithFrame:CGRectMake(0, 430, 320, 50)];
    [navigationController.view addSubview:bottomToolBar];

    //[window addSubview:navigationController.view];
    [window makeKeyAndVisible];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationSelector:) name:@"tab_clicked" object:Nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationSelector:) name:@"share_btn_trigger" object:Nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationSelector:) name:@"search_action_trigger" object:Nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationSelector:) name:@"comment_trigger" object:Nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationSelector:) name:@"login_start" object:Nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationSelector:) name:@"login_success"object:Nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationSelector:) name:@"login_fail" object:Nil];
    
    NSString *md5str = [UtilClass md5FromString: @"zlwwakin_19842012-2-22 15:12:36"];
    NSString *readyStr = [[NSString alloc] initWithFormat:@"2012-2-22 15:12:36$%@",md5str];
    NSString *secret_str = [UtilClass Base64EncodeString:readyStr];
    
    NSString *utf8QueryStr = (NSString *)CFURLCreateStringByAddingPercentEscapes(
                                                                                 NULL,
                                                                                 (CFStringRef)secret_str,
                                                                                 NULL,
                                                                                 (CFStringRef)@"!*'();:@&=+$,/?%#[]",
                                                                                 kCFStringEncodingUTF8 );
    
    //NSString *theUrl = [[NSString alloc] initWithFormat:@"%@%@",WEB_SERVICE_1,utf8QueryStr];
    
    NSLog(@"MD5:%@ SECRET STR:%@ UTF-8 SOLUTION:%@",md5str,secret_str,utf8QueryStr);
    
    searchPanel = [[SearchPanel alloc] initWithFrame:CGRectMake(320, pos_y, 320, 500)];
    [searchPanel initWithTags:[[NSArray alloc] initWithObjects:@"搜作品",@"搜人物",@"搜机构", nil]
     suffix:@"search_panel"];
    [window addSubview:searchPanel];
    
    commentPanel = [[CommentPanel alloc] initWithFrame:CGRectMake(320,pos_y, 320, 480)];
    [commentPanel initWithSuffix:@"comment_panel"];
    //[commentPanel displayComment:@"37962"];
    [window addSubview:commentPanel];
    
    infoPanel = [[InfoPanel alloc] initWithFrame:CGRectMake(100, 130, 320, 480)];
    infoPanel.hidden = TRUE;
    [window addSubview:infoPanel];
    
	return YES;
}

-(void) notificationSelector:(NSNotification *) notification
{
    NSString *curNotificationName = [notification name];
    //NSLog(@"--------------");
    
    if ([curNotificationName isEqualToString:@"tab_clicked"])
    {
        ImageUIButton *tempBtn =  [notification object];
        
        if (tempBtn != Nil) 
        {
            [self hideAllUIs];

            if (tempBtn.button_id == 0) 
            {
                newView.hidden = FALSE;
            }
            else if (tempBtn.button_id == 1) 
            {
                worksView.hidden = FALSE;
            }
            else if (tempBtn.button_id == 2) 
            {
                peopleView.hidden = FALSE;
            }
            else if (tempBtn.button_id == 3) 
            {
                jigouView.hidden = FALSE;   
            }
            else if (tempBtn.button_id == 4) 
            {
                moreView.hidden = FALSE;
            }
        }

    }
    else if([curNotificationName isEqualToString:@"share_btn_trigger" ])
    {
        [self showSharekit];
    }
    else if([curNotificationName isEqualToString:@"search_action_trigger"])
    {
        [searchPanel shiftIn];
    }
    else if([curNotificationName isEqualToString:@"comment_trigger"])
    {
        NSString *theId =  [notification object];
        [commentPanel displayComment:theId];//theId]; 

    }
    else if([curNotificationName isEqualToString:@"login_start"])
    {
        infoPanel.hidden  = FALSE;
        [infoPanel startLogin];
    }
    else if([curNotificationName isEqualToString:@"login_success"])
    {
        [infoPanel loginSuccess];
    }
    else if([curNotificationName isEqualToString:@"login_fail"])
    {
        infoPanel.hidden = TRUE;
    }
}

-(void) showSharekit
{
    SHKItem *item = [SHKItem text:@"test string share 3"];
    //SHKItem *item = [SHKItem image:[UIImage imageNamed:@"dolphin_logo_114.png"] title:@"share image test"];
    //SHKItem *item = [SHKItem URL:[NSURL URLWithString:@"http://weibo.com/2670941224/profile?leftnav=1&wvr=3.6"] title:@"share url test"];
    
	SHKActionSheet *actionSheet = [SHKActionSheet actionSheetForItem:item];
    actionSheet.destructiveButtonIndex = 0;
	[actionSheet showFromToolbar:navigationController.toolbar]; 
}

-(void) actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0) //share to weibo
    {
        NSLog(@"SHARE TO WEIBO");
    }
}

- (void)applicationWillTerminate:(UIApplication *)application 
{
	// Save data if appropriate
}

#pragma mark -
#pragma mark Memory management

-(void) hideAllUIs
{
    newView.hidden = TRUE;
    worksView.hidden = TRUE;
    peopleView.hidden = TRUE;
    jigouView.hidden = TRUE;
    moreView.hidden = TRUE;
}

- (void)dealloc {
	[navigationController release];
	[window release];
	[super dealloc];
}

+(BOOL) need_draw_logo_m
{
    return need_draw_logo;
}

+(void) set_need_draw_logo:(BOOL) in_draw_logo
{
    need_draw_logo = in_draw_logo;
}
@end


@implementation UINavigationBar (CustomImage)

- (void)drawRect:(CGRect)rect
{
    UIColor *color = [ColorUtil stringHexToUIColor:@"0x112033"];//[UIColor redColor];
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColor(context, CGColorGetComponents( [color CGColor]));
    CGContextFillRect(context, rect);
    
    NSLog(@"draw rect");
    
    //if ([AsyncImagesAppDelegate need_draw_logo_m]) 
    {
        UIImage *image = [UIImage imageNamed: @"zl_small_logo.png"];
        [image drawInRect:CGRectMake(70, 8, 26, 26)];
    }

}
@end




